/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Inicial.java
 *
 * Created on 27-nov-2011, 21:11:03
 */
package Principal.produccion;

import java.math.BigDecimal;
import java.math.RoundingMode;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import librerias.existencias.ListaDobleConOrden_Existencia;

/**
 *
 * @author JONATHAN
 */
public class Frame_Plan_de_Produccion extends javax.swing.JFrame {
    double promedio = 0,H_extra,H_Ordinaria;
    public DefaultTableModel miModelo;
    public String[][] data = {};
    public int elementos_periodo,num_trabajadores;
    static ListaDobleConOrden_Existencia ldco = new ListaDobleConOrden_Existencia();

    JTable getMiTabla() {
        return Tabla;
    }

    public DefaultTableModel getMiModelo() {
        return miModelo;
    }
    /** Creates new form Inicial */
    public Frame_Plan_de_Produccion() {
        initComponents();
        setLocationRelativeTo(null);
        Num_meses.setValue(1);
        miModelo = new DefaultTableModel(data, new String[]{""}) {
        };
        Tabla.setModel(miModelo);
        jScrollPane1.setViewportView(Tabla);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txt_CHOrdinaria = new javax.swing.JTextField();
        txt_CMantenimiento = new javax.swing.JTextField();
        txt_CHExtra = new javax.swing.JTextField();
        txt_TiempoPxU = new javax.swing.JTextField();
        txt_NumeHOr = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Tabla = new javax.swing.JTable();
        Num_meses = new javax.swing.JSpinner();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Plan de Produccion");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("Costo de hora ordinaria");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText("Número de horas diarias");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setText("Costo de hora extra");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setText("Tiempo de produccion por unidad");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setText("Costo de mantenimiento");

        Tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Mes", "Pronostico (Unidades)", "Dias laborables por mes"
            }
        ));
        jScrollPane1.setViewportView(Tabla);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel6.setText("Número de meses");

        jButton1.setText("Generar Tabla");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jMenu1.setText("Ejecutar");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem1.setText("Mostrar Formato");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(17, 17, 17)
                                    .addComponent(jLabel3))))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txt_CHExtra, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txt_CMantenimiento, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txt_CHOrdinaria, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2)
                            .addComponent(jLabel6))
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(Num_meses)
                            .addComponent(txt_NumeHOr)
                            .addComponent(txt_TiempoPxU, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 345, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(55, 55, 55)
                        .addComponent(jButton1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(txt_NumeHOr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txt_TiempoPxU, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(txt_CHOrdinaria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel1))
                                .addGap(32, 32, 32)
                                .addComponent(txt_CHExtra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel3))
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txt_CMantenimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Num_meses, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5))))
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        elementos_periodo = Integer.parseInt(Num_meses.getValue().toString());
    if (elementos_periodo != 0) {
        Object[] titulos = new Object[3];
        titulos[0] = "Mes";
        titulos[1] = "Pronostico (unidades)";
        titulos[2] = "Dias Laborables";
        getMiModelo().setColumnIdentifiers(titulos);
        getMiModelo().setColumnCount(3);
        getMiModelo().setRowCount(elementos_periodo);

        for (int i = 0; i < getMiModelo().getRowCount(); i++) {
            //getMiTabla().setValueAt(i + 1, i, 0);
            getMiTabla().setValueAt(i+1, i, 0);
        }

    }
    
}//GEN-LAST:event_jButton1ActionPerformed

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:
    Revisar(txt_TiempoPxU,txt_NumeHOr);
    //GENERAR_TABLA();
}//GEN-LAST:event_jMenuItem1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Frame_Plan_de_Produccion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Frame_Plan_de_Produccion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Frame_Plan_de_Produccion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Frame_Plan_de_Produccion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Frame_Plan_de_Produccion().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSpinner Num_meses;
    private javax.swing.JTable Tabla;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txt_CHExtra;
    private javax.swing.JTextField txt_CHOrdinaria;
    private javax.swing.JTextField txt_CMantenimiento;
    private javax.swing.JTextField txt_NumeHOr;
    private javax.swing.JTextField txt_TiempoPxU;
    // End of variables declaration//GEN-END:variables

    private void GENERAR_TABLA() {
        double suma_H_Hombre=0,suma_H_Linea=0;
         for (int j = 0; j < getMiModelo().getRowCount(); j++) {
            Object[] fila = new Object[3];
            for (int k = 0; k < getMiModelo().getColumnCount(); k++) {
                fila[k] = getMiModelo().getValueAt(j, k);
                //System.out.println("..." + fila[k]);
            }
             try {
                 suma_H_Hombre += Double.parseDouble(fila[1].toString()) * Double.parseDouble(txt_TiempoPxU.getText());
                 suma_H_Linea += Double.parseDouble(fila[2].toString()) * Double.parseDouble(txt_NumeHOr.getText());
             System.out.println(fila[0]+"   "+fila[1]+"  "+fila[2]+"  "+Double.parseDouble(fila[1].toString())*Double.parseDouble(txt_TiempoPxU.getText())+"   "+Double.parseDouble(fila[2].toString())*Double.parseDouble(txt_NumeHOr.getText()));
             } catch (Exception numberFormatException) {
                 JOptionPane.showMessageDialog(null, "Los campos no admiten vacio");
             }
         }
         double N_trab=suma_H_Hombre/suma_H_Linea;
         double entero_alto=Math.ceil(N_trab);
         double entero_bajo=Math.floor(N_trab);
         double opcion1,opcion2;
         opcion1=(suma_H_Hombre-entero_bajo*suma_H_Linea)*H_extra;
         opcion2=(entero_alto*suma_H_Linea-suma_H_Hombre)*H_Ordinaria;
         System.out.println("Al entero prox"+entero_alto+" Truncado"+entero_bajo);
         System.out.println("Opcion1 "+(suma_H_Hombre-entero_bajo*suma_H_Linea)*H_extra);
         System.out.println("Opcion2 "+(entero_alto*suma_H_Linea-suma_H_Hombre)*H_Ordinaria);
         if(opcion1<opcion2){
             System.out.println("usar horas extras");
             num_trabajadores=(int)entero_bajo;
         }
         else{
             System.out.println("usar horas ordinarias");
             num_trabajadores=(int)entero_alto;
         }
         
    }

    private void Revisar(JTextField txt_TiempoPxU, JTextField txt_NumeHOr) {
        if(txt_NumeHOr.getText().isEmpty() || txt_TiempoPxU.getText().isEmpty()
             ||txt_CHExtra.getText().isEmpty() || txt_CHOrdinaria.getText().isEmpty()
                ||txt_CMantenimiento.getText().isEmpty()
                ){  
            JOptionPane.showMessageDialog(null, "Los campos no admiten vacio");
        }
        else{
            H_Ordinaria=Double.parseDouble(txt_CHOrdinaria.getText());
            H_extra=Double.parseDouble(txt_CHExtra.getText())-Double.parseDouble(txt_CHOrdinaria.getText());
            System.out.println("Costo H ordinaria"+H_Ordinaria+" Costo H_Extra"+H_extra);
        GENERAR_TABLA();
        System.out.println("Generando Informe para "+num_trabajadores+" Trabajadores.......................");
        }
    }
}
